package com.project.service;

import com.project.common.utils.Constants;
import com.project.entity.SchoolManagerInfo;
import com.project.entity.User;
import com.project.entity.UserRole;
import com.project.myBeans.SchoolManagerDetail;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: TateBrown
 * @date: 2018/5/26 15:01
 * @param:
 * @return:
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class SchoolMangerServiceImpl extends BaseService implements SchoolMangerService {
    @Autowired
    private UserService userService;
    @Autowired
    private SchoolManagerInfoService schoolManagerInfoService;
    @Autowired
    private UserRoleService userRoleService;

    /**
     * author: WheelChen
     * date: 2017/8/8
     * function: 创建学院管理员账号
     *
     * @param schoolManagerDetail user表 + schoolManagerInfo表
     * @return
     */
    @Override
    public boolean addAccount(SchoolManagerDetail schoolManagerDetail) throws Exception {
        boolean flag = false;
        User user = schoolManagerDetail.getUser();
        SchoolManagerInfo schoolManagerInfo = schoolManagerDetail.getSchoolManagerInfo();
        userService.createUser(user);
        if (user.getId() != null){

            //插入用户角色表
            UserRole userRole = new UserRole();
            userRole.setRoleid(Constants.USERTYPE.SCHOOL.value());//学院管理员
            userRole.setUserid(user.getId());
            userRoleService.add(userRole);

            schoolManagerInfo.setUserid(user.getId());
            schoolManagerInfoService.addOne(schoolManagerInfo);
            flag = true;
        }
        return flag;
    }

    /**
     * author: WheelChen
     * date: 2017/10/10
     * function: 根据学院id查找学院管理员用户表信息
     *
     * @param schoolId
     * @return
     */
    @Override
    public User findOneBySchoolId(Integer schoolId) throws Exception {
        SchoolManagerInfo schoolManagerInfo = schoolManagerInfoService.findOneBySchoolId(schoolId);
        if (schoolManagerInfo == null) return null;
        return userService.findOneAllByUserID(schoolManagerInfo.getUserid());
    }

    /**
     * author: WheelChen
     * date: 2017/10/30
     * function: 根据学院id删除学院管理员（用户表 + 学院管理员信息表）
     *
     * @param schoolId 学院id
     * @return
     */
    @Override
    public boolean removeOneBySchoolId(Integer schoolId) {
        SchoolManagerInfo schoolManagerInfo = schoolManagerInfoService.findOneBySchoolId(schoolId);
        if (schoolManagerInfo != null){
            //删除用户表
            userService.removeOneById(schoolManagerInfo.getUserid());
            //删除学院管理员信息表
            schoolManagerInfoService.removeOneBySchoolId(schoolId);
        }
        return true;
    }

    /**
     * author: WheelChen
     * date: 2017/10/30
     * function: 根据用户id删除学院管理员（用户表 + 学院管理员信息表）
     *
     * @param userId 用户表id
     * @return
     */
    @Override
    public boolean removeOneByUesrId(Integer userId) {
        //删除用户表
        userService.removeOneById(userId);
        //删除学院管理员信息表
        schoolManagerInfoService.removeOneByUserId(userId);
        return true;
    }
}
